const { router } = dva;
const { Link } = router;
const { Icon, Menu, Breadcrumb } = antd;
const { SubMenu, Item } = Menu;
const CheckUtils = require('./CheckUtils.jsx');
const StringUtils = require('./StringUtils.jsx');

const breadCrumbRouteMap = require('../Route.jsx').breadCrumbMap;// 路由的面包屑数据
const breadCrumbMenuMap = new Map();// 菜单的面包屑数据

const fn = {
    // 生成菜单（递归）
    genMenu(datas, level) {
        const menus = [];
        datas.forEach((data, index) => {
            const hasPath = CheckUtils.isEmpty(data.path), key = hasPath ? data.key : data.path;
            if (data.children && data.children.length > 0) {// 非根节点
                !hasPath ? breadCrumbMenuMap.set(data.path, { label: data.label, notClick: true }) : '';
                menus.push(<SubMenu key={key} title={<span><Icon type={data.icon}/><span>{data.label}</span></span>}>
                    {this.genMenu(data.children, level + 1)}
                </SubMenu>);
            } else {
                !hasPath ? breadCrumbMenuMap.set(data.path, { label: data.label, notClick: false }) : '';
                menus.push(<Item key={key}>
                    <Link to={data.path} style={{ paddingTop: 2 }}>
                        {data.icon ? <Icon type={data.icon}/> : ""}<span>{data.label}</span>
                    </Link>
                </Item>);
            }
        });
        return menus;
    },
    getbreadCrumb(pathNames) {// 获取面包屑items，数据由菜单和路由提供
        const { Item } = Breadcrumb, items = [];
        pathNames.forEach(value => {
            const { label, notClick } = breadCrumbMenuMap.get(value) || { label: breadCrumbRouteMap.get(value) };
            if(!label)
                return;
            items.push(<Item key={StringUtils.generateUUID()}>{ notClick ? label : <Link to={value}>{label}</Link> }</Item>);
        });
        return items;
    }
};

module.exports = fn;